﻿using System;
using System.Collections.Generic;
using System.Windows.Navigation;
using MugenMvvmToolkit.Interfaces;

namespace MugenMvvmToolkitTest.TestInfrastructure
{
    public class TestNavigationService : INavigationService
    {
        #region Properties

        public Func<Uri, bool> NavigateToUri { get; set; }
        public Func<IViewPageMappingItem, bool> NavigateToMapping { get; set; }
        public Func<object, object, bool> NavigateToSource { get; set; }
        public Func<bool> NavigateToRoot { get; set; }

        #endregion

        #region Implementation of INavigationService

        /// <summary>
        ///     Gets or sets the root page <see cref="IViewPageMappingItem" />
        /// </summary>
        public IViewPageMappingItem RootItem { get; set; }

        /// <summary>
        ///     The <see cref="Uri" /> source.
        /// </summary>
        public Uri Source { get; set; }

        /// <summary>
        ///     Indicates whether the navigator can navigate back.
        /// </summary>
        public bool CanGoBack { get; set; }

        /// <summary>
        ///     Indicates whether the navigator can navigate forward.
        /// </summary>
        public bool CanGoForward { get; set; }

        /// <summary>
        ///     The current <see cref="Uri" /> source.
        /// </summary>
        public Uri CurrentSource { get; set; }

        /// <summary>
        ///     The current content.
        /// </summary>
        public object CurrentContent { get; set; }

#if WINDOWS_PHONE
        /// <summary>
        ///     Gets an IEnumerable that you use to enumerate the entries in back navigation history.
        /// </summary>
        /// <returns>List of entries in the back stack.</returns>
        public IEnumerable<JournalEntry> BackStack { get; private set; }

        /// <summary>
        ///     Removes the most recent entry from the back stack.
        /// </summary>
        /// <returns> The entry that was removed. </returns>
        public JournalEntry RemoveBackEntry()
        {
            throw new NotImplementedException();
        }
#endif

        /// <summary>
        ///     Stops the loading process.
        /// </summary>
        public void StopLoading()
        {
        }

        /// <summary>
        ///     Navigates back.
        /// </summary>
        public void GoBack()
        {
        }

        /// <summary>
        ///     Navigates forward.
        /// </summary>
        public void GoForward()
        {
        }

        /// <summary>
        ///     Displays the content located at the specified <see cref="IViewPageMappingItem" />.
        /// </summary>
        /// <param name="uri">
        ///     The <c>Uri</c> of the content to display.
        /// </param>
        /// <returns>
        ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
        /// </returns>
        public bool Navigate(Uri uri)
        {
            return NavigateToUri(uri);
        }

        /// <summary>
        ///     Displays the content located at the specified <see cref="IViewPageMappingItem"/>.
        /// </summary>
        /// <returns>
        ///     <c>true</c> if the content was successfully displayed; otherwise, <c>false</c>.
        /// </returns>
        /// <param name="source">
        ///     The <c>IViewPageMappingItem</c> of the content to display.
        /// </param>
        bool INavigationService.Navigate(IViewPageMappingItem source)
        {
            return NavigateToMapping(source);
        }

        /// <summary>
        ///     Navigates to the root page.
        /// </summary>
        bool INavigationService.NavigateToRoot()
        {
            return NavigateToRoot();
        }

        public event NavigatedEventHandler Navigated;
        public event NavigatingCancelEventHandler Navigating;
        public event NavigationFailedEventHandler NavigationFailed;
        public event NavigationStoppedEventHandler NavigationStopped;
        public event FragmentNavigationEventHandler FragmentNavigation;

        #endregion

        #region Methods

        public virtual void OnNavigated(NavigationEventArgs e)
        {
            var handler = Navigated;
            if (handler != null) handler(this, e);
        }

        public virtual void OnNavigating(NavigatingCancelEventArgs e)
        {
            var handler = Navigating;
            if (handler != null) handler(this, e);
        }

        #endregion
    }
}